//
//  NetworkCommand.m
//  ZhangJiangJun
//
//  Created by FENGYL on 2017/1/18.
//  Copyright © 2017年 FENGYL. All rights reserved.
//

#import "NetworkCommand.h"
#import "UserModel.h"
#import "NSString+MD5.h"

#import <AFNetworking.h>
#import <MBProgressHUD.h>

@implementation NetworkCommand

#pragma mark 登录
+(void)userLoginUserName:(NSString *)userName password:(NSString *)password success:(void(^)())success failure:(void(^)())failure{
    
    NSMutableDictionary * parameters = [ NSMutableDictionary dictionary];
    parameters[NC_USER_GUID] = [self getUniqueStrByUUID];
    parameters[NC_TERMINAL_MODEL] = @"ffffffff";
    parameters[NC_PLAT_FORM] = @"IOS";
    parameters[NC_ACCOUNT_TYPE] = @3;
    parameters[NC_USER_NAME] = userName;
    
    NSString * newPassword = [NSString stringWithFormat:@"HYZ%@%@",userName,password];
    
    NSString * md5Password = [NSString stringToMD5:newPassword];
    
    parameters[NC_USER_PASSWORD] = md5Password;
    
    
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    
    NSString * postUrl = [NSString stringWithFormat:@"%@%@",NC_SERVICE_URL,NC_LOGON];
    
    [manager POST:postUrl parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        //登录失败
        if (!([[responseObject valueForKey:@"ClientType"] intValue] > 0)) {
            
            failure();
            
            return;
            
        }
        
        //存返回信息
        UserModel * userModel = [[UserModel alloc]init];
        
        userModel.userID = [responseObject valueForKey:@"Id"];
        
        userModel.clientType = [responseObject valueForKey:@"ClientType"];
        
        userModel.tokenPass = [responseObject valueForKey:@"TokenPass"];
        
        userModel.userName = [responseObject valueForKey:@"UserName"];
        
        
        
        
        NSLog(@"success %@",responseObject);
        
        
        success();
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
        NSLog(@"%@",error);
        
        
        failure();
        
        
        
    }];
    
}

#pragma mark 获取设备列表
+(void)getDeviceListSuccess:(void(^)())success failure:(void(^)())failure{
    
    UserModel * userModel = [[UserModel alloc]init];
    
    NSMutableDictionary * parameters = [ NSMutableDictionary dictionary];
    parameters[NC_USER_ID] = userModel.userID;
    parameters[NC_CLIENT_TYPE] = userModel.clientType;
    parameters[NC_TOKEN_PASS] = userModel.tokenPass;
    
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    
    NSString * postUrl = [NSString stringWithFormat:@"%@%@",NC_SERVICE_URL,NC_GET_DEVICES_BY_ACCOUNT];
    
    [manager POST:postUrl parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
    
        
        userModel.deviceByAccountArray = responseObject;
        
        
        NSLog(@"success %@",responseObject);
        
        
        success();
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
        NSLog(@"%@",error);
        
        
        failure();
        
        
        
    }];
    
}

#pragma mark 历史报警
+(void)getHistoryEventDataByTime:(NSDate *)date success:(void(^)())success failure:(void(^)())failure{
    
    UserModel * userModel = [[UserModel alloc]init];
    
    //获取开始结束时间
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    [dateFormatter setDateFormat:@"YYYY-MM-dd"];
    
    NSString *stringDate = [dateFormatter stringFromDate:date];
    
    NSString *startTime = [NSString stringWithFormat:@"%@ 00:00:00",stringDate];
    
    NSString *endTime = [NSString stringWithFormat:@"%@ 23:59:59",stringDate];
    
    //
    
    NSMutableDictionary * parameters = [ NSMutableDictionary dictionary];
    parameters[NC_USER_ID] = userModel.userID;
    parameters[NC_CLIENT_TYPE] = userModel.clientType;
    parameters[NC_TOKEN_PASS] = userModel.tokenPass;
    parameters[NC_SERIAL_NUMBER] = userModel.currentDeviceSerialNumber;
    parameters[NC_START_TIME] = [self getTimeStampForDateString:startTime];
    parameters[NC_END_TIME] = [self getTimeStampForDateString:endTime];
    
    
    
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    
    NSString * postUrl = [NSString stringWithFormat:@"%@%@",NC_SERVICE_URL,NC_GET_HISTORY_EVENT_DATA_BY_TIME];
    
    [manager POST:postUrl parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        userModel.deviceHistoryEventDataByTimeArray = responseObject;
        
        NSLog(@"success %@",responseObject);
        
        
        success();
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
        NSLog(@"%@",error);
        
        
        failure();
        
        
        
    }];
    
}

#pragma mark 上锁解锁
+(void)stampIsUnlock:(BOOL)isUnlock stampCount:(NSString *)stampCount time:(NSString *)time success:(void(^)())success failure:(void(^)())failure{
    
    UserModel * userModel = [[UserModel alloc]init];
    
    NSMutableDictionary * parameters = [ NSMutableDictionary dictionary];
    parameters[NC_USER_ID] = userModel.userID;
    parameters[NC_CLIENT_TYPE] = userModel.clientType;
    parameters[NC_TOKEN_PASS] = userModel.tokenPass;
    parameters[NC_SERIAL_NUMBER] = userModel.currentDeviceSerialNumber;
    
    if (isUnlock) {
        
        parameters[NC_COMMAND_TEXT] = [NSString stringWithFormat:@"*gzn*%@*%@*",stampCount,time];
        
    }else{
        
        parameters[NC_COMMAND_TEXT] = @"*gzn*0*";
        
    }
    
    
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    
    NSString * postUrl = [NSString stringWithFormat:@"%@%@",NC_SERVICE_URL,NC_PUT_CHAT_COMMAND];
    
    [manager POST:postUrl parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        
        
        NSLog(@"success %@",responseObject);
        
        
        success();
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
        NSLog(@"%@",error);
        
        
        failure();
        
        
        
    }];
    
}

#pragma mark 获取相应时间内设备警报经纬度
+(void)getDeviceLocationByTime:(NSDate *)date Success:(void (^)())success failure:(void (^)())failure{
    
    UserModel * userModel = [[UserModel alloc]init];
    
    //获取开始结束时间
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    [dateFormatter setDateFormat:@"YYYY-MM-dd"];
    
    NSString *stringDate = [dateFormatter stringFromDate:date];
    
    NSString *startTime = [NSString stringWithFormat:@"%@ 00:00:00",stringDate];
    
    NSString *endTime = [NSString stringWithFormat:@"%@ 23:59:59",stringDate];
    
    //
    
    NSMutableDictionary * parameters = [ NSMutableDictionary dictionary];
    parameters[NC_USER_ID] = userModel.userID;
    parameters[NC_CLIENT_TYPE] = userModel.clientType;
    parameters[NC_TOKEN_PASS] = userModel.tokenPass;
    parameters[NC_SERIAL_NUMBER] = userModel.currentDeviceSerialNumber;
    parameters[NC_START_TIME] = [self getTimeStampForDateString:startTime];
    parameters[NC_END_TIME] = [self getTimeStampForDateString:endTime];
    
    
    
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    
    NSString * postUrl = [NSString stringWithFormat:@"%@%@",NC_SERVICE_URL,NC_GET_HISTORY_TRACK_POI_DATA_BY_TIME];
    
    [manager POST:postUrl parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        userModel.deviceHistoryEventLocationByTimeArray = responseObject;
        
        NSLog(@"success %@",responseObject);
        
        
        success();
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
        NSLog(@"%@",error);
        
        
        failure();
        
        
        
    }];
}

#pragma mark 时间戳转换时间
+ (NSString *)TimestampChangeTimestr:(NSString *)Timestamp
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    [dateFormatter setDateFormat:@"HH:mm:ss"];
    
    NSLog(@"CurTimestamp =  %@",Timestamp);
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[Timestamp doubleValue]];
    NSLog(@"Greenwich time  = %@",confromTimesp);
    NSString *confromTimespStr = [dateFormatter stringFromDate:confromTimesp];
    NSLog(@"CurTime =  %@",confromTimespStr);
    return confromTimespStr;
}

#pragma mark 根据日期和时间返回时间戳
+(NSNumber *)getTimeStampForDateString:(NSString *)dateString{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    NSDate *date = [formatter dateFromString:dateString]; //------------将字符串按formatter转成nsdate
    
   
    
    NSNumber * num = @([date timeIntervalSince1970]);
    
    NSLog(@"timeSp:%@",num); //时间戳的值
    
    
    return num;
}


#pragma mark 获取GUID
+ (NSString *)getUniqueStrByUUID
{
    CFUUIDRef    uuidObj = CFUUIDCreate(nil);//create a new UUID
    
    //get the string representation of the UUID
    
    NSString    *uuidString = (__bridge_transfer NSString *)CFUUIDCreateString(nil, uuidObj);
    
    CFRelease(uuidObj);
    
    return uuidString ;
    
}


@end
